package com.li.algorithm.leetcode;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

public class Test1_10 {
    /**
     * 检查两个字符串数组是否相等
     */
    public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
        StringBuilder str1 = new StringBuilder();
        StringBuilder str2 = new StringBuilder();
        for (String s : word1) {
            str1.append(s);
        }
        for (String s : word2) {
            str2.append(s);
        }
        return str1.compareTo(str2) == 0;
    }

    /**
     * 回文数
     */
    public boolean isPalindrome(int x) {
        int num = x;
        int y = 0;
        if (x < 0)  //负数直接返回false
            return false;
        if (x < 10) //小于10直接返回true
            return true;
        while (x >= 1) {
            y = y * 10 + x % 10;
            x = x / 10;
        }
        return num == y;
    }

    /**
     * 字符串转换整数
     */
    public int myAtoi(String s) {
        boolean flag = false; //是否为负数
        boolean num = false; //是否已经出现了数字
        long result = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 48 && c <= 57) {
                result = result * 10 + (c - 48);
                num = true;
            } else {
                if (num) {
                    return flag ? (int)-result : (int)result;
                }
                if (c == '+' || c == '-') {
                    num = true;
                    flag = c == '-';
                } else if (c != ' ') //一开始就遇到其它字符，直接结束
                    return 0;
            }
            if (!flag && result > Integer.MAX_VALUE)
                return Integer.MAX_VALUE;
            if (flag && -result < Integer.MIN_VALUE)
                return Integer.MIN_VALUE;
        }
        return flag ? (int)-result : (int)result;
    }

    /**
     * 整数反转
     */
    public int reverse(int x) {
        int y = 0;
        long result = 0;
        boolean flag = true; //默认为正数
        if (x < 0) {
            flag = false;
            x = Math.abs(x);
        }
        while (x >= 1) {
            y = x % 10;
            result = result * 10 + y;
            x = x / 10;
            if (result > Integer.MAX_VALUE)
                return 0;
        }
        if (flag) return (int)result;
        else return (int)-result;
    }

    /**
     * 字形变换
     * @param s
     * @param numRows
     * @return result
     */
    public String convert(String s, int numRows) {
        int pointy;
        StringBuilder result = new StringBuilder();
        if (numRows == 1)
            return s;
        //每个字符都遍历一遍，每次输出当前字符开头的所有字符，输出过的字符进行标记
        for (int i = 0; i < numRows; i++) {
            int point = i;
            while (point < s.length()) {
                char colChart = s.charAt(point);
                result.append(colChart);
                //如果不是第一行和最后一行
                if (i != 0 && i != numRows - 1) {
                    pointy = point  + (numRows - (i + 1)) * 2;
                    if (pointy < s.length() && pointy > 0) {
                        char midChart = s.charAt(pointy);
                        result.append(midChart);
                    }
                }
                point = point + numRows * 2 - 2;
            }
        }
        return result.toString();
    }

    /**
     * 最长回文字串
     */
    public String longestPalindrome(String s) {
        if (s == null || s.length() < 2) {
            return s;
        }
        int strlen = s.length();
        int maxStart = 0;
        int maxEnd = 0;
        int maxLen = 1;
        boolean[][] dp = new boolean[strlen][strlen];
        for (int r = 1; r < strlen; r++) {
            for (int l = 0; l < r; l++) {
                //如果左右字符相同,并且当前字符串长度<=2 || 前一个字串为回文
                if (s.charAt(l) == s.charAt(r) && (r - l <= 2 || dp[l + 1][r - 1])) {
                    dp[l][r] = true;
                    if (r - l + 1 > maxLen) {
                        maxLen = r - l + 1;
                        maxStart = l;
                        maxEnd = r;
                    }
                }
            }
        }
        return s.substring(maxStart, maxEnd + 1);
    }

    /**
     * 寻找两个正序数组的中位数
     */
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        LinkedList<Integer> list = new LinkedList<>();
        for (int num : nums1) {
            list.add(num);
        }
        for (int num : nums2) {
            list.add(num);
        }
        list.sort(Integer::compareTo);
        int size = list.size();
        return size % 2 != 0 ? list.get((size - 1) / 2) : (double) (list.get(size/2 - 1) + list.get(size/2))/2;
    }

    /**
     * 无重复的最长字串
     */
    public int lengthOfLongestSubstring(String s) {
        int dataCount = 0;
        int flag = 0;
        Set<Character> set = new HashSet<>();
        for (int i = 0; i < s.length(); i++) {
            for (int j = i; j < s.length(); j++) {
                dataCount = j - i + 1;
                set.add(s.charAt(j));
                if (set.size() != dataCount || j == s.length() - 1) {
                    if (set.size() > flag) {
                        flag = set.size();
                    }
                    break;
                }
            }
            dataCount = 0;
            set.clear();
        }
        return flag;
    }

    /**
     * 罗马数字转整数
     */
    public int getValue(char c) {
        char[][] numbers = {
                {'I', 'V', 'X', 'L','C', 'D', 'M'},
                {1, 5, 10, 50, 100, 500, 1000},
        };
        for (int i = 0; i < numbers[0].length; i++) {
            if (numbers[0][i] == c) {
                return numbers[1][i];
            }
        }
        return 0;
    }
    public int romanToInt(String s) {
        char[] chars = s.toCharArray();
        int result = 0;
        for (int i = 0; i < chars.length; i++) {
            int value = getValue(chars[i]);
            //如果当前字符小于后续字符，当前字符置为负数
            if (i < chars.length - 1 && value < getValue(chars[i+1])) {
                value = -value;
            }
            result = result + value;
        }
        return result;
    }

    /**
     * 正则表达式匹配
     * @param s
     * @param p
     * @return
     */
    public boolean isMatch(String s, String p) {
        char pre = 0;
        char[] sChars = s.toCharArray();
        char[] pChars = p.toCharArray();
        for (int i = 0, j = 0; i < s.length() && j < p.length(); i++) {
            if (pChars[j] == '*') {

            }
            if (pChars[j] == '.') {

            }
            if (pChars[j] == sChars[i]) {

            }


        }
        return true;
    }


    public static void main(String[] args) {
        Test1_10 test = new Test1_10();

    }
}
